#!/usr/bin/env python
#-*- coding=utf-8 -*-

import urllib
import urllib2
import platform

#---------------------------------------------------------------
# 根据url获取网页正文
def get_webpage(url):
    headers = {\
            'User-Agent' : 'Mozilla/5.0 (X11; Linux i686; rv:34.0) Gecko/20100101 Firefox/34.0',\
            'Accept'     : 'text/html',\
            'Connection' : 'keep-alive'}
    try:
        request = urllib2.Request(url, None, headers)
        response = urllib2.urlopen(request, timeout=120)
        webpage = response.read()
        response.close()
        return webpage

    #except urllib2.HTTPError, e:
    #    print('HTTPError: ' + str(e.code))
    #except urllib2.URLError, e:
    #    print('URLError: ' + str(e.reason))
    except Exception, e:
        print('发生异常: ' + str(e))

#将平台编码转换成unicode
def p2u(text):
    plat_str = platform.system()
    if plat_str == 'Windows':
        return text.encode('GBK')
    else:
        return text
    pass

#将unicode转换成平台编码
def u2p(text):
    plat_str = platform.system()
    if plat_str == 'Windows':
        return text.decode('GBK')
    else:
        return text
    pass

